home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / JOHNLOVE / C_SOURCE / CMYTEAR.C < prev    next >
Text File  |  1992-02-29  |  8KB  |  232 lines

  1. /*********************************************************
  2.  "CmyTear.c"
  3.  
  4.  by John A. Love, III [Washington Apple Pi Users' Group]
  5.  
  6.  using Symantec's "THINK C", v 5.0.1
  7.  ... as derived from their "TCL Starter" files
  8.  *********************************************************/
  9.  
  10.  
  11.  
  12.  
  13. #include "CmyTear.h"
  14.  
  15.  
  16. extern CBureaucrat        *gGopher;
  17. extern short            gClicks;
  18. extern CDesktop            *gDesktop;
  19.  
  20.  
  21.  
  22. void    CCustomSelector::DrawItem (short theItem, Rect *theBox)        {
  23. /* Passed area rect is in QuickDraw coordinates */
  24.  
  25.         #define kMAC    3000
  26.         Boolean            savedAlloc;
  27.         PicHandle        myPic;
  28.         Rect            pictRect;
  29.         
  30.         
  31.     switch (theItem)    {
  32.         
  33.         case 1:
  34.             savedAlloc = SetAllocation(kAllocCanFail);
  35.             myPic = GetPicture(kMAC);
  36.             pictRect = (**myPic).picFrame;
  37.             SetAllocation(savedAlloc);
  38.             FailNILRes(myPic);
  39.     
  40.             CenterRects(&pictRect, theBox);
  41.             DrawPicture(myPic, &pictRect);
  42.     
  43.             ReleaseResource(myPic);            /* Do NOT need after drawing. */
  44.             break;
  45.             
  46.         default:
  47.             inherited::DrawItem(theItem, theBox);    // Empty for now.
  48.             break;
  49.             
  50.     }    /* only 1 item */
  51.  
  52. }    /* DrawItem */
  53.  
  54.  
  55.  
  56. void    CCustomSelector::DoClick (Point hitPt, short modifierKeys, long when)    {
  57.  
  58.         short    itemHit, count;
  59.         long    finalTicks;
  60.         
  61.         
  62.     itemHit = FindItem(hitPt);
  63.     
  64.     // Ignore all clicks beyond 2:
  65.     if ( (itemHit != selection) && (gClicks == 2) )        {
  66.         for (count = 2; count > 0; count--)        {
  67.             HiliteItem(itemHit, hiliteON);
  68.             Delay(20, &finalTicks);
  69.             HiliteItem(itemHit, hiliteOFF);
  70.             Delay(20, &finalTicks);
  71.         }
  72.         gDesktop->Prepare();        // ??????????
  73.         itsSupervisor->DoCommand(-(( (long)commandBase <<16 ) + itemHit));
  74.     }
  75.     
  76. }    /* DoClick */
  77.  
  78.  
  79.  
  80. void    CTearMenuDir::ITearMenuDir (CApplication *aSupervisor)        {
  81.  
  82.         #define kTearMenu    1000
  83.         #define kMAC        3000
  84.         CCustomSelector        *myMenu;
  85.         CSelectorMDEF        *myMDEF;
  86.         Boolean                savedAlloc;
  87.         PicHandle            myPic;
  88.         Rect                pictRect;
  89.         
  90.     
  91.     /* Retrieve window with ID = kTearMenu and set = itsWindow from
  92.     ** CTearOffMenu's superclass = CDirector.  Pass "this" as the
  93.     ** window's Supervisor.  "this" = CmyApp's instance variable =
  94.     ** itsTearMenu of Class = CTearOffMenu because the ORIGINAL
  95.     ** object that sent us here to begin with was itsTearMenu.
  96.     **
  97.     ** "A director is a bureaucrat that supervises a window".  The
  98.     ** above is consistent with this because CTearOffMenu descends
  99.     ** from CDirector.  Down a little bit we will make the supervisor
  100.     ** of the CGridSelector (is a CSelector is a CPanorama is a CPane)
  101.     ** our application.  Normally, the supervisor of a Pane is a doc.  */
  102.     
  103.     inherited::ITearOffMenu(aSupervisor, kTearMenu);
  104.     
  105.     savedAlloc = SetAllocation(kAllocCanFail);
  106.     myPic = GetPicture(kMAC);
  107.     SetAllocation(savedAlloc);
  108.     FailNILRes(myPic);
  109.     pictRect = (**myPic).picFrame;
  110.     OffsetRect(&pictRect, -pictRect.left, - pictRect.top);    // Zero origin.
  111.     ReleaseResource(myPic);            /* Do NOT need after getting frame. */
  112.     
  113.     myMenu = new (CCustomSelector);
  114.     myMenu->IGridSelector(
  115.                             itsWindow,            /* From superclass of CTearOffMenu */
  116.                             aSupervisor,        /* My application */
  117.                             0, 0,
  118.                             sizELASTIC, sizELASTIC,
  119.                             0,                    /* Initial item to select */
  120.                             kTearMenu,            /* Command Base */
  121.                             1, 1,                /* # of rows & columns */
  122.                             pictRect.right + 2,    /* box width & height */
  123.                             pictRect.bottom + 2
  124.                          );
  125.     myMenu->SetGridOn(false);    // IGridSelector sets = true.
  126.     myMenu->FitToEnclosure(true, true);
  127.     myMenu->Activate();
  128.         
  129.     itsPane = myMenu;        /* myMDEF->DrawMenu calls itsPane->Draw */
  130.  
  131.     /* Reset the minimum & maximum sizes of a window.
  132.     ** TCL has these preset = 100 & GrayRgn's rgnBBox: */
  133.     SetRect(&itsWindow->sizeRect,
  134.             pictRect.left    - 1,
  135.             pictRect.top    - 1,
  136.             pictRect.right    + 3,
  137.             pictRect.bottom    + 3);
  138.             
  139.     /* Change the size of all the window's sub-view(s)
  140.     ** which is now just my CGridSelector = myMenu.
  141.     ** Leave a blank 1 pixel border around PICTure
  142.     ** so PICTure does NOT touch the window frame:        */
  143.     itsWindow->ChangeSize(pictRect.right + 2, pictRect.bottom + 2);
  144.     
  145.     /* Leave same blank space in setting margins.  This
  146.     ** margin ALSO includes the regular window frame.
  147.     ** So we have a 1 pixel blank space, a 1 pixel frame
  148.     ** AND a 10 pixel drag bar on top:                    */
  149.     SetRect(&margins, 2, 12, 2, 2);
  150.     
  151.     myMDEF = new (CSelectorMDEF);
  152.     myMDEF->ISelectorMDEF(kTearMenu, myMenu, this);
  153.     
  154.     itsWindow->MoveOffScreen();            // Keep handy, but out of the way. */
  155.     itsWindow->Select();
  156.     
  157.     /* ISelectorMDEF initializes its superclass by calling IPaneMDEF which
  158.     ** calls IMenuDefProc(kTearMenu) to fill-in the zero-bytes of attached
  159.     ** 'MDEF' resource.  This latter method sets:
  160.     **            (**theMDEF).itsMenuDefProc = this;
  161.     ** where "this" = the ORIGINAL object that was sent the IMenuDefProc
  162.     ** message.  The originator = myMDEF since myMDEF sent ISelectorMDEF
  163.     ** which sent IMenuDefProc.  So, for example, when the GenericMDEF
  164.     ** calls:
  165.     **            itsMenuDefProc->DrawMenu(...);
  166.     ** myMDEF->DrawMenu really gets called which method CSelectorMDEF
  167.     ** inherits from CPaneMDEF.  Ditto for myMDEF->SizeMenu.
  168.     ** myMDEF->ChooseItem is a method directly within CSelectorMDEF.
  169.     ** This ChooseItem method determines whether you╒ve instigated a
  170.     ** tear by testing if you dragged to the side or just downward as
  171.     ** you╒d normally do to just choose an item.
  172.     **
  173.     ** The DrawMenu routine of CPaneMDEF calls itsPane->Draw where
  174.     ** "itsPane" is an instance variable of CPaneMDEF.  Note above
  175.     ** that I╒ve stuffed a CGridSelector object {which is a CSelector
  176.     ** is a CPanorama which descends from CPane} into the "itsPane"
  177.     ** belonging to CTearOffMenu.  Same pane, but different objects
  178.     ** or Classes.  HOW ???
  179.     **
  180.     ** Below we pass CTearOffMenu's "itsPane" to ISelectorMDEF which, after
  181.     ** it calls IMenuDefProc, calls IPaneMDEF thereby initializing CSelectorMDEF's
  182.     ** superclass.  IPaneMDEF stores the passed CTearOffMenu's "itsPane" in
  183.     ** CPaneMDEF╒s "itsPane".  IPaneMDEF also stuffs the passed CTearOffMenu
  184.     ** object (= "this" = last parm passed to ISelectorMDEF) into CPaneMDEF's
  185.     ** "itsTearOffMenu".
  186.     **
  187.     ** myMDEF->DrawMenu, then, effectively calls CPandMDEF::DrawMenu which
  188.     ** calls itsPane->Draw where "itsPane" = myMenu of class CGridSelector.
  189.     ** So ... CGridSelector::Draw gets called and it draws all the items in
  190.     ** your grid by calling CGridSelector::DrawItem.  TCL's "DrawItem" method
  191.     ** is empty, so you MUST override it.
  192.     **
  193.     ** CPaneMDEF::SizeMenu retrieves the width & height of CPaneMDEF::itsPane
  194.     ** and stuffs them into the MENU fields = menuWidth & menuHeight.
  195.     **
  196.     ** CSelectorMDEF::ChooseItem, as I have previously stated, tests for a
  197.     ** tear-off by following your dragging motion.  If you╒ve dragged beyond
  198.     ** the confines of the pulled-down Menu and NOT stayed within them (as you
  199.     ** would for a normal choose), and if CPaneMDEF::itsTearOffMenu is not NULL
  200.     ** and if the Menu itself is enabled THEN we call the TearOffMenu
  201.     ** method which CSelectorMDEF inherited from CPaneMDEF.  This last method
  202.     ** calls CPaneMDEF::itsTearOffMenu->TornOff which simply creates a
  203.     ** new(CTearChore) and initializes it.  We pass ╥this╙ to ITearChore,
  204.     ** meaning the object that was sent the message = TornOff.  So, ╥this╙
  205.     ** = CPaneMDEF::itsTearOffMenu which ITearChore stores in the latter's
  206.     ** "itsTearOffMenu".  The new CTearChore is then passed to:
  207.     **                    gApplication->AssignUrgentChore(newTearChore);
  208.     **
  209.     ** Your app will then call newTearChore->Perform which calls:
  210.     **                    CTearChore::itsTearOffMenu->MoveToCorner();
  211.     ** MoveToCorner eventually calls _MoveWindow passing true as the update parm.
  212.     ** An update Event occurs and myMenu->Draw gets called because myMenu is
  213.     ** a sub-view of the torn-off window.                                        */
  214.  
  215. }    /* ITearMenuDir */
  216.  
  217.  
  218.  
  219. void    CTearMenuDir::DoCommand (long theCommand)        {
  220.         
  221.         
  222.     if (HiShort(-theCommand) == ((CGridSelector*)itsPane)->GetCommandBase())
  223.             gGopher->DoCommand(theCommand);
  224.     else    inherited::DoCommand(theCommand);
  225.  
  226. }    /* DoCommand */
  227.  
  228.  
  229.  
  230.  
  231. /* end: "CmyTear.c" */
  232.